package socket.udp.unicast;

import java.io.IOException;
import java.net.*;
import java.util.Scanner;

/**
 * @author 周宇
 * @create 2020-05-25 17:59
 * • UDP发送数据：数据来自于键盘录入，知道输入的数据是886，发送数据结束
 * • UDP接收数据：因为接收端不知道发送端什么时候停止发送，故采用死循环接收
 */
public class SendClient {
    public static void main(String[] args) throws IOException {
        DatagramSocket ds = new DatagramSocket();
        Scanner in = new Scanner(System.in);
        while (true) {
            String s = in.nextLine();
            if (s.equals("886")) {
                break;//跳出
            }
            byte[] bytes = s.getBytes();
            InetAddress address = InetAddress.getByName("192.168.2.118");
            int port = 1883;
            DatagramPacket dp = new DatagramPacket(bytes, bytes.length, address, port);
            ds.send(dp);
        }
        ds.close();
    }
}
